﻿namespace Cos.Module.CMS.NHibernate.Test
{
    using System;
    using System.Collections.Generic;

    using Cos.Core.NHibernate;
    using Cos.Core.NHibernate.Tests;

    using NUnit.Framework;

    public class ModuleFixtureBase : FixtureBase
    {
        public CultureDataModel Culture { get; private set; }
        public VideoDataModel Video { get; private set; }
        public TemplateDataModel Template { get; private set; }
        public NavDataModel Nav { get; private set; }
        public PageDataModel Page { get; private set; }
        public ArchivePageDataModel ArchivePage { get; private set; }
        public DataTypeDataModel DataType { get; private set; }
        public PartDataModel Part { get; private set; }

        public ModuleFixtureBase()
            : base(new TestModel())
        {
            this.Culture = new CultureDataModel
            {
                Id = "fr-CA",
                CultureDesc = string.Empty,
                IsDefault = true
            };

            this.Video = new VideoDataModel
            {
                IsExternal = false,
                OriginalCultureCode = "fr-CA",
                //VideoCultures = new List<VideoCultureDataModel>(),
                //VideoTags = new List<VideoTagDataModel>()
            };

            this.Template = new TemplateDataModel
            {
                IsListView = false,
                TemplateName = "Template",
                TemplateUrl = "url"
            };

            this.Nav = new NavDataModel
            {
                InheritPermissions = true,
                NavOrder = 1,
                ParentId = 0
            };

            this.Page = new PageDataModel
            {
                ApprobationDate = new DateTime(2011, 01, 06),
                Author = "author",
                CreationDate = new DateTime(2011, 01, 06),
                CultureCode = this.Culture.Id,
                LastModified = new DateTime(2011, 01, 06),
                PageName = "page name",
                PageNamePreview = "page name preview",
                PageNavEnd = true,
                PageStatus = "Approved",
                PageVisibility = true,
                PageVisibilityPreview = true
            };

            this.ArchivePage = new ArchivePageDataModel
                {
                    ArchiveDate = new DateTime(2006, 01, 01),
                    //ArchiveElements = new List<ArchiveElementDataModel>(),
                    Author = "Admin",
                    CultureCode = "En-CA",
                    NavId = 234,
                    PageName = "page1",
                    IsPageNavEnd = true,
                    IsPageVisible = true,
                    IsPageVisibilityPreview = true
                };

            this.DataType = new DataTypeDataModel
                {
                    DataTypeName = "abcdefgh" + Guid.NewGuid(),
                    HasMedia = true,
                    IsFormField = true
                };

            this.Part = new PartDataModel
            {
                PartName = "part name",
                PartOrder = 1,
                IsMultiCulture = true,
                IsRequired = false,
                CustomValidator = "no",
                MaxValue = "1",
                MinValue = "1"
            };
        }

        [TestFixtureSetUp]
        public void ModuleSetupContext()
        {
            using (var tx = this.Session.BeginTransaction())
            {
                this.Session.SaveOrUpdate(this.Template);
                this.Session.SaveOrUpdate(this.Culture);
                this.Session.SaveOrUpdate(this.DataType);
                this.Session.SaveOrUpdate(this.Video);
                this.Nav.TemplateId = this.Template.Id;
                this.Session.SaveOrUpdate(this.Nav);
                this.Page.NavId = this.Nav.Id;
                this.Session.SaveOrUpdate(this.Page);
                this.Session.SaveOrUpdate(this.ArchivePage);
                this.Part.TemplateId = this.Template.Id;
                this.Part.DataTypeId = this.DataType.Id;
                this.Session.SaveOrUpdate(this.Part);

                tx.Commit();
            }

            this.Session.Flush();
            this.Session.Clear();
        }
    }
}
